package edu.zut.cs.software.cms.module.student.service.impl;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import edu.zut.cs.software.cms.base.service.impl.GenericManagerImpl;
import edu.zut.cs.software.cms.module.student.dao.StudentAreaDao;
import edu.zut.cs.software.cms.module.student.dao.StudentDao;
import edu.zut.cs.software.cms.module.student.domain.Student;
import edu.zut.cs.software.cms.module.student.domain.StudentArea;
import edu.zut.cs.software.cms.module.student.service.StudentManager;



/**
 * ManagerImpl for Student
 * 
 * @author 任雪燕
 *
 */
@Service("studentManager")
public class StudentManagerImpl extends GenericManagerImpl<Student, Long> implements StudentManager {

	StudentDao studentDao;
	
	StudentAreaDao studentAreaDao;
	
	@Autowired
	public void setStudentDao(StudentDao studentDao) {
		this.studentDao = studentDao;
		this.dao = this.studentDao;
	}
	
	@Autowired
	public void setStudentAreaDao(StudentAreaDao studentAreaDao) {
		this.studentAreaDao = studentAreaDao;
	}
	
	
//	@Override
//	public List<Student> getList() {
//		return studentDao.selectStudents();
//	}
//	
	@Override
	public Student getStudentById(Long id) {
		return studentDao.selectById(id);
	}
    
    @Override
	public boolean IfExist(String code) {
		//数据存在返回false
		//数据不存在返回true
		if((studentDao.IfOneExist(code))>0) 
			return false;
		else
			return true;
	}
    
	@Override
	public void CreateOne(String code,String name,String gender,String address,String tel,String email,String department,String major,String clazz) {
		Student student = new Student();
		StudentArea studentArea = new StudentArea();
		
		student.setCode(code);
		student.setName(name);
		student.setGender(gender);
		student.setAddress(address);
		student.setTel(tel);
		student.setEmail(email);
		
		studentArea.setDepartment(department);
		studentArea.setMajor(major);
		studentArea.setClazz(clazz);
		
		studentArea.setParent(studentArea);
		student.setStudentArea(studentArea);
		
		studentDao.save(student);
		studentAreaDao.save(studentArea);
	}


	@Override
	public List<Student> getList() {
		return (List<Student>)this.dao.findAll();
	}
	
	@Override
	public Optional<Student> findByStudentId(Long studentId) {
		Optional<Student> result=studentDao.findById(studentId);
		return result;
	}
	
	
	@Override
	public Boolean deleteById(Long studentId) {
		Optional<Student> student=this.dao.findById(studentId);
		if(student.equals(Optional.empty())) {
			return false;
		}else {
			this.dao.delete(student.get());
			return true;
		}
		
	}


	
}
